/*
 * @lc app=leetcode.cn id=387 lang=javascript
 *
 * [387] 字符串中的第一个唯一字符
 */

// @lc code=start
/**
 * @param {string} s
 * @return {number}
 */
var firstUniqChar = function(s) {
  let map = {}
  for (let i = 0; i < s.length; i++) {
    let item = s[i]
    if (map[item] !== undefined) {
      map[item].count += 1
    } else {
      map[item] = {
        i,
        count: 1
      }
    }
  }
  let res = null
  for (const item in map) {
    if (Object.hasOwnProperty.call(map, item)) {
      const element = map[item];
      if (element.count === 1) {
        if (!res) {
          res = element
        } else if (res.i > element.i) {
          res = element
        }
      }
    }
  }
  return res ? res.i : -1
};
// @lc code=end

